package de.hauschild.orbitvm.vm.instruction.impl.dooble;

import de.hauschild.orbitvm.vm.VM;
import de.hauschild.orbitvm.vm.instruction.impl.AbstractInstruction;

public class Phi extends AbstractInstruction {

  private final int firstArgumentAdress;
  private final int secondArgumentAdress;

  public Phi(final int theInstructionCounter, final int theFirstArgumentAdress, final int theSecondArgumentAdress) {
    super(theInstructionCounter);
    firstArgumentAdress = theFirstArgumentAdress;
    secondArgumentAdress = theSecondArgumentAdress;
  }

  @Override
  public void evaluate(final VM vm) {
    if (vm.getStatus()) {
      vm.getMemory()[getInstructionCounter()] = vm.getMemory()[firstArgumentAdress];
    } else {
      vm.getMemory()[getInstructionCounter()] = vm.getMemory()[secondArgumentAdress];
    }
  }

  @Override
  public String toString() {
    return String.format("%d: Phi (%d, %d)", getInstructionCounter(), firstArgumentAdress, secondArgumentAdress);
  }
}
